﻿using System;
using System.Threading;

using SBPweb.PhotoGallery.Common;

namespace SBPweb.PhotoGallery.Client.Logic
{

    public class AutoAdvancingGallery : AdvancingGallery
    {

        #region Private fields

        private bool isActive;
        private TimeSpan defaultDelay = TimeSpan.FromMilliseconds(3000);

        #endregion

        #region Constructor logic

        public AutoAdvancingGallery()
            : base()
        {
        }

        public AutoAdvancingGallery(string basePath)
            : base(basePath)
        {
        }

        public AutoAdvancingGallery(FetchableSlide[] slides, string basePath)
            : base(slides, basePath)
        {
        }

        public AutoAdvancingGallery(FetchableSlide[] slides, string basePath, int? pageIndex, int? pageSize)
            : base(slides, basePath, pageIndex, pageSize)
        {
        }

        #endregion

        #region Property accessors

        public bool IsAutoAdvanceActive
        {
            get
            {
                if (base.AutoPlay.HasValue)
                {
                    if (!base.AutoPlay.Value)
                    {
                        return false;
                    }
                }
                return isActive;
            }
            set
            {
                if (base.AutoPlay.HasValue)
                {
                    if (!base.AutoPlay.Value)
                    {
                        throw new Exception("AutoAdvancing (AutoPlay) feature is disabled for this Gallery!");
                    }
                }
                isActive = value;
                if (value)
                {
                    FetchCurrentSlide();
                }
            }
        }

        public TimeSpan DefaultAdvanceDelay
        {
            get
            {
                return defaultDelay;
            }
            set
            {
                defaultDelay = value;
            }
        }

        #endregion

        #region Methods

        protected override void OnImageFetching(object sender, SlideFetchProgressEventArgs e)
        {
            base.OnImageFetching(sender, e);
        }

        protected override void OnImageFetched(object sender, SlideFetchEventArgs e)
        {
            base.OnImageFetched(sender, e);
            if (isActive)
            {
                int delay = (int)defaultDelay.TotalMilliseconds;
                if (e.Slide.EndDelay.HasValue)
                {
                    delay = (int)e.Slide.EndDelay.Value;
                }
                Timer timer = new Timer(new TimerCallback(TimerElapsed), this, delay, Timeout.Infinite);
            }
        }

        #region Event handlers

        private static void TimerElapsed(object sender)
        {
            AutoAdvancingGallery aag = sender as AutoAdvancingGallery;
            if (aag.isActive)
            {
                aag.FetchNextSlide();
            }
        }

        #endregion

        #endregion

    }

}